package cn.org.timespace.easyframe.action.permission;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import cn.org.timespace.easyframe.action.EFBaseAction;
import cn.org.timespace.easyframe.model.permission.EfDictionary;
import cn.org.timespace.easyframe.model.permission.EfDictionaryId;
import cn.org.timespace.easyframe.service.permission.EfDictionaryService;

public class EfDictionaryAction extends EFBaseAction {
	 
	private static final long serialVersionUID = -5372255585999694728L;

	private EfDictionaryService efDictionaryService;

	public EfDictionaryService getEfDictionaryService() {
		return efDictionaryService;
	}

	public void setEfDictionaryService(EfDictionaryService efDictionaryService) {
		this.efDictionaryService = efDictionaryService;
	}

	// 该构造函数实现
	public EfDictionaryAction() {
		super(new EfDictionaryModel());
	}

	private EfDictionaryModel getEfDictionaryModel() {
		return (EfDictionaryModel) this.getModel();
	}

	EfDictionaryModel efDictionaryModel = (EfDictionaryModel) this.getModel();

	// 查所有
	public String doList() {
		// EfDictionaryId id=new EfDictionaryId("Catering", "*");
		List efList = efDictionaryService.findByCondition(efDictionaryModel);
		getRequest().setAttribute(VOLIST, efList);
		return LIST;
	}

	public String doSave() {
		EfDictionary efDictionary = efDictionaryModel.getEfDictionary();
		efDictionaryService.save(efDictionary);
		return doList();
	}

	public String doUpdate() {
		EfDictionary efDictionary = efDictionaryModel.getEfDictionary();
		efDictionaryService.update(efDictionary);
		return doList();
	}

	public String doFindById() {
		
		String groupName = getRequest().getParameter("groupName");
		String itemValue = getRequest().getParameter("itemValue");
		
		//增加
		if(StringUtils.isEmpty(groupName) && StringUtils.isEmpty(itemValue)){
			return ADD;
		}
		else
		{
			EfDictionaryId id = new EfDictionaryId();
			id.setGroupname(groupName);
			id.setItemvalue(itemValue);
			EfDictionary efDictionary = efDictionaryService.findById(id);
			efDictionaryModel.setEfDictionary(efDictionary);
			return UPDATE;
		}
	}

	public String doDelete() {
		String groupName = getRequest().getParameter("groupName");
		String itemValue = getRequest().getParameter("itemValue");
		EfDictionaryId id = new EfDictionaryId();
		id.setGroupname(groupName);
		id.setItemvalue(itemValue);
		EfDictionary efDictionary = efDictionaryService.findById(id);
		efDictionaryService.delete(efDictionary);
		return doList();
	}
}
